<?php
/* 
 * Copyright (c) 2011 Antoine d'Otreppe de Bouvette
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 */

class Config implements ArrayAccess {
    const INI_PARENT = '.parent';
    
    /**
     *
     * @var array
     */
    private $items;
    
    /**
     *
     * @param string $filename 
     * @return Config
     */
    public static function createFromIniFile($filename) {
        return new Config(inheritIni($filename));
    }
    
    public function __construct(array $items=array()) {
        $this->items = $items;
    }
    
    public function required(/* $varargs */) {
        foreach (func_get_args() as $item) {
            if (!array_key_exists($item, $this->items)) {
                throw new RuntimeException(
                    "Required config item '$item' is missing in ".
                    get_class($this));
            }
        }
    }
    
    /**
     * @param string $filename 
     * @return Config this object
     */
    public function mergeWithIniFile($filename) {
        $this->items = array_merge(
                $this->items,
                inheritIni($filename));
        
        return $this;
    }
    
    public function mergeWithArray(array $array) {
        $this->items = array_merge($this->items, $array);
        return $this;
    }
    
    /**
     *
     * @param string $filename 
     * @return Config this object
     */
    public function mergeWithJsonFile($filename) {
        
    }
    
    /**
     * @param mixed $offset
     * @return boolean
     */
    public function offsetExists($offset) {
        return array_key_exists($offset, $this->items);
    }
    
    /**
     * @param mixed $offset 
     */
    public function offsetGet($offset) {
        return array_get($this->items, $offset);
    }
    
    public function offsetSet($offset, $value) {
        $this->readOnly();
    }
    
    public function offsetUnset($offset) {
        $this->readOnly();
    }
    
    public function getArray($offset) {
        $string = $this->offsetGet($offset);
        
        if ($string === null) {
            return null;
        }
        else {
            return json_parse($string);
        }
    }
    
    private function readOnly() {
        throw new BadMethodCallException("The config is read-only");
    }
}
